package com.xinlong.dao;

import com.xinlong.entity.Permission;
import com.xinlong.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRolesdao {

    @Select("select * from role where id in(select roleId from users_role where userId=#{id})")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "roleName",property = "roleName"),
            @Result(column = "roleDesc",property = "roleDesc"),
            @Result(column = "id",property = "permissions",javaType = List.class,many = @Many(select = "com.xinlong.dao.IpermissionsDao.findbyroleid")),
    })
    List<Role> findbyusername(String id);

    @Select("select * from role")
    List<Role> findall();

    @Insert("insert into role(roleName,roleDesc) value(#{roleName},#{roleDesc})")
    void save(Role role);

    @Select("select * from role where id =#{id}")
    Role findbyid(String id);

    @Select("select * from permission")
    List<Permission> findRoleByIdAndAllPermission2(String id);

    @Select("select * from permission where id not in (select permissionId from role_permission where roleId=#{roleId})")
    List<Permission> findRoleByIdAndAllPermission(String id);

    @Insert("insert into role_permission (roleId,permissionId) value (#{roleId},#{permissionId}) ")
    void addRoleTopermissin(@Param("roleId") String roleid, @Param("permissionId")String id);

    @Delete("delete from role_permission where roleId=#{id}")
    void delRoleTopermissin(Integer id);
}
